﻿// <copyright file="ExportForm.CSV.cs" company="BerlinSoft">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>Wernfried Schwenkner</author>
// <email>wernfried@schwenkner.de</email>
// <date>2009-10-21</date>
// <summary>
// Contains the class of ExportForm for CSV export.
// </summary>

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;


namespace SenseTimeTracker.Export
{
	/// <summary>
	/// Class implements the export form for csv export
	/// </summary>
	public partial class ExportForm
	{
		/// <summary>
		/// Exports the time records within a given period into csv format
		/// </summary>
		/// <param name="date">DateTime where the given period must be contained in</param>
		/// <param name="exportPeriod">Period to be exported</param>
		public void ExportToCSV(DateTime date, ExportPeriod exportPeriod)
		{
			DateTime dateBegin;
			DateTime dateFinal;
			TimeTrackerXML timeCards = GetTimeCards(date, exportPeriod, out dateBegin, out dateFinal);
			TextWriter writer = new StreamWriter(DetermineFilename(dateBegin, dateFinal, "csv", exportPeriod == ExportPeriod.Week));
			writer.WriteLine("ClientName,ClientType,ProjectName,ProjectNumber,ProjectType,TaskName,TaskNumber,TaskType,TaskLocation,TaskEstimate,StartTime,FinalTime,Description,Details");
			foreach (TimeTrackerXML.Client client in timeCards.Clients)
			{
				foreach (TimeTrackerXML.Project project in client.Projects)
				{
					foreach (TimeTrackerXML.Task task in project.Tasks)
					{
						foreach (TimeTrackerXML.TimeCard timeCard in task.TimeCards)
						{
							writer.WriteLine(
								client.Name + "," +
								client.Type + "," +
								project.Name + "," +
								project.Number + "," +
								project.Type + "," +
								task.Name + "," +
								task.Number + "," +
								task.Type + "," +
								task.Location + "," +
								task.Estimate + "," +
								timeCard.StartTime + "," +
								timeCard.FinalTime + "," +
								timeCard.Description + "," +
								timeCard.Details);
						}
					}
				}
			}

			writer.Close();
		}
	}
}
